package day8.Exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionDemo {

    public static void main(String[] args) {
//        test1();

//        test2();
        test3();
    }

    private static void test3() {
        Scanner sc = new Scanner(System.in);
        System.out.println("先输入一个数字");

        int num = sc.nextInt();

        System.out.println("再输入另外一个数字:");
        int num2 = sc.nextInt();

        try {
            int num3 = num/num2;
            System.out.println(num + "/" + num2 + "=" + num3);

            int[] arr = {3,5,8};
            System.out.println(arr[3]);
            return;
        } catch (InputMismatchException | ArithmeticException | ArrayIndexOutOfBoundsException e) {//这种写法不多见，这是并列异常
            //e.printStackTrace();//不建议隐藏这错误
            e.printStackTrace();
            //test2();
        }finally {
            //不要在这里返回
            //最后执行的代码块，一般作为释放释放资源，关闭流，对象
            System.out.println("这是最后执行的");
        }
    }

    private static void test2() {

        Scanner sc = new Scanner(System.in);
        System.out.println("先输入一个数字");

        int num = sc.nextInt();

        System.out.println("再输入另外一个数字");
        int num2 = sc.nextInt();

        try {
            int num3 = num/num2;
            System.out.println(num + "/" + num2 + "=" + num3);

            int[] arr = {3, 5, 8};
            System.out.println(arr[3]);
            return;
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } catch (ArithmeticException e){
            e.printStackTrace();
            System.out.println("算术异常...");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("其它异常...");
        } finally {
            System.out.println("这是最后执行的");
        }

    }

    private static void test1() {
        Scanner sc = new Scanner(System.in);
        System.out.println("先输入一个数字");

        int num = sc.nextInt();

        System.out.println("再输入另外一个数字");
        int num2 = sc.nextInt();

        int num3 = num/num2;
        System.out.println(num + "/" + num2 + "=" + num3);
    }
}
